/**
 * 
 */
package microcredit.gui.table.impl;

import java.io.File;
import java.net.MalformedURLException;

import javax.swing.ImageIcon;

import microcredit.gui.table.EnumBeanTableModel;
import microcredit.model.Report;

enum ReportColumns {
	COLUMN_NAME,
	COLUMN_FILE;
}

public final class ReportTableModel extends EnumBeanTableModel<Report, ReportColumns> {

	private final ImageIcon rowIcon;
	
	public ReportTableModel() {
		this.rowIcon = new ImageIcon(ReportTableModel.class.getResource("/microcredit/icons/x16/chart.png"));
	}
	
	@Override
	protected Object getValue(Report bean, ReportColumns column) {
		switch (column) {
		case COLUMN_NAME : return bean.getName();
		case COLUMN_FILE : return bean.getJasperFile();
		}
		return null;
	}
	
	@Override
	protected ImageIcon getRowIcon(Report o) {
		if (o.getIcon() != null) {
			File f = new File("./reports/", o.getIcon());
			if (f .exists()) {
				try {
					return new ImageIcon(f.toURI().toURL());
				} catch (MalformedURLException e) {
				}
			}
		}
		
		return super.getRowIcon(o);
	}

	@Override
	protected String getName(ReportColumns column) {
		switch (column) {
		case COLUMN_NAME : return  "Name";
		case COLUMN_FILE : return "File";
		}
		return "";
	}

	@Override
	public ImageIcon getRowIcon() {
		return rowIcon;
	}
	
}